<template>
  <div>
    <h3>商品列表</h3>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            value
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="7">
          <el-button type="primary" @click="qchurch">清除</el-button>
          <el-button type="primary"  @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="95px"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="95px"
        ></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="140px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="125px">
          <template v-slot="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="deleteclear(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 3, 4, 5]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      //保存商品列表信息
      goodsList: [],
      //总数据条数
      total: 0,
    };
  },
  methods: {
    // 点击触发添加事件
    goAddPage(){
        this.$router.push("/add")
    },
    // deleteclear删除按钮
    // 根据作用域找到当前的index的id
    // 判断是否按下了confirm确定键 如果按下的confirm是否存在 ，如果不存在就是取消，存在就发送删除请求
    //  如果以后重新渲染页面
   async deleteclear(id){
       const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
          if(confirmResult !== "confirm"){
             return this.$message.info('已经取消删除')
          }
         //没有取消就是要删除，发送请求完成删除
         const { data: res } = await this.$http.delete(`goods/${id}`)
         if(res.meta.status !==200) return this.$message.error("删除失败")
          this.$message.success('删除成功')
          this.getGoodsList()
    },
    // 清除
    qchurch() {
      this.queryInfo.query = "";
    },
    // 获取数据
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      console.log(res);
      this.goodsList = res.data;
      if (res.meta.status !== 200) {
        this.$message.error("获取是失败");
      }
      //  console.log(res.data);
      this.goodsList = res.data.goods;
      this.total = res.data.total;
      this.$message.success("获取成功");
      //  this.getGoodsList();
    },
    // 当前分页改变的时候触发
    handleSizeChange(val1) {
      // console.log(val);
      //当页号发生改变时，更改pagesize，重新请求
      this.queryInfo.pagesize = val1;
      this.getGoodsList();
    },
    // 当前条数发生改变时
    handleCurrentChange(val2) {
      //当页码发生改变时，更改pagesize，重新请求
      this.queryInfo.pagenum = val2;
      this.getGoodsList();
    },
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created() {
    this.getGoodsList();
  },
  mounted() {},
};
</script>

<style scoped lang='less'>
</style>
